package org.absolutezero.client.view;

import java.util.List;

import javax.inject.Inject;

import org.absolutezero.client.style.HeadingLabel;
import org.absolutezero.client.util.CellFactory;
import org.absolutezero.client.util.Dialog;
import org.absolutezero.client.util.FormFactory;
import org.absolutezero.client.util.Session;
import org.absolutezero.shared.RefrigeratorRequestFactory;
import org.absolutezero.shared.ShelfProxy;
import org.absolutezero.shared.ShelfRequest;
import org.absolutezero.shared.UserProxy;
import org.absolutezero.shared.UserRequest;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.google.web.bindery.requestfactory.shared.Request;
import com.google.web.bindery.requestfactory.shared.ServerFailure;

public class ShelvesView extends Composite {

	private static ShelvesViewUiBinder uiBinder = GWT
			.create(ShelvesViewUiBinder.class);

	private final static HeadingLabel headingMy = new HeadingLabel("My shelves");
	private final static HeadingLabel headingPublic = new HeadingLabel("Browse public shelves");
	
	interface ShelvesViewUiBinder extends UiBinder<Widget, ShelvesView> {
	}

	@UiField
	HTMLPanel content;
	
	private final EventBus ebus;
	private final RefrigeratorRequestFactory requestFactory;
	
	@Inject
	public ShelvesView(EventBus ebus,
				RefrigeratorRequestFactory requestFactory) {
		
		this.ebus = ebus;
		this.requestFactory = requestFactory;
		
		// niepotrzebne - setStyle jest w konstruktorze
//		headingMy.setStyleName("heading");
//		headingPublic.setStyleName("heading");
		
		initWidget(uiBinder.createAndBindUi(this));
	}

	public void setMyShelves(){
		content.clear();
		
		content.add(headingMy);
		
		final VerticalPanel panel = new VerticalPanel();
		final Button btnNewShelf = new Button("Add Shelf");
		btnNewShelf.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				setEditShelf();
			}
			
		});
		
		UserProxy user = (UserProxy)Session.get("logged-user");
		UserRequest request = requestFactory.userRequest();
		user = request.edit(user);
		Request<List<ShelfProxy>> getUserRequest = request.fetchShelves().using(user);
		
		getUserRequest.fire(new Receiver<List<ShelfProxy>>(){

			@Override
			public void onSuccess(List<ShelfProxy> response) {
				CellTable<ShelfProxy> table = CellFactory.getMyShelvesTableView(response);
				panel.add(table);
				panel.add(btnNewShelf);
			}
			
			@Override
			public void onFailure(ServerFailure f){
				Dialog.show("Shelves fetch failed",f.getMessage());
			}
			
		});
		
		
		
		content.add(panel);
		
	}
	
	public void setBrowseShelves(){
		content.clear();
		
		content.add(headingPublic);
		
		ShelfRequest request = requestFactory.shelfRequest();
		Request<List<ShelfProxy>> fetchShelvesReq = request.getAllShelves().with("owner");
		
		fetchShelvesReq.fire(new Receiver<List<ShelfProxy>>(){

			@Override public void onSuccess(List<ShelfProxy> response) {
				
				CellTable<ShelfProxy> table = CellFactory.getBrowseShelvesTableView(response);
				content.add(table);
			}
			@Override
			public void onFailure(ServerFailure f){
				Dialog.show("Shelves fetch failed",f.getMessage());
			}
			
		});
	}
	
	public void setEditShelf(){
		content.clear();
		
		Panel p = FormFactory.getEditShelf(null,requestFactory);
		content.add(p);
	}
	
	public void setEditShelf(ShelfProxy shelf){
		content.clear();
		
		Panel p = FormFactory.getEditShelf(shelf,requestFactory);
		content.add(p);
	}
	
	public void setEditForeignShelf(ShelfProxy shelf){
		content.clear();
		
		Panel p = FormFactory.getEditShelf(shelf,requestFactory,false);
		content.add(p);
	}
	
	public void setViewShelf(ShelfProxy shelf){
		content.clear();
		VerticalPanel vp = new VerticalPanel();
		vp.add(new HeadingLabel(shelf.getName()));
		Panel p = FormFactory.getViewShelf(shelf, requestFactory);
		vp.add(p);
		content.add(vp);
	}
	
}
